
global output output
set scheme s1mono

*---------------------------------------*
*PROGRAM TO COMPARE CUT 1 TO CUT 2
*---------------------------------------*
cap program drop comparecut12
program define comparecut12 
syntax [, load(str) source(str) outcome(str) ylab(str) ]

if "`ylab'" == "none" local ylab 
else local ylab `ylab' 

if "`outcome'" == "any_dis" global ytit Any Disability
if "`outcome'" == "any_ssissdi" global ytit Any SSI or SSDI
if "`outcome'" == "any_worklim" global ytit Any Signif. Disability
if "`outcome'" == "dis_tot" global ytit Disability (Dollars)
if "`outcome'" == "ssissdi_tot" global ytit SSI or SSDI (Dollars)

global dis_tot_fmt %9.0fc
global ssissdi_tot_fmt %9.0fc

foreach x in  any_dis any_ssissdi any_worklim    {
	global `x'_fmt %5.2f
}

global loader ./output/main-estimates/`outcome'/graphdata_`outcome'

		use "$loader" if y<=19, clear
		drop if y == -2
		replace y = y-.1
		gen y2 = y+.2
		local model 2sls_quad
		
			foreach cut in 31 50 {
				gen ub_cut`cut'_`model' = beta_cut`cut'_`model' + 1.96*se_cut`cut'_`model'
				gen lb_cut`cut'_`model' = beta_cut`cut'_`model' - 1.96*se_cut`cut'_`model'
			}	
			
			tw 	(rcap ub_cut31_`model' lb_cut31_`model' y, lcolor(black) lp(dash) lw(thin))	///
				(line beta_cut31_`model' y, lcolor(black) lp(dash) lw(medthick))	///
				(rcap ub_cut50_`model' lb_cut50_`model' y2, lcolor(gs7) lw(thin))	///
				(line beta_cut50_`model' y2, lcolor(gs7) lw(medthick))	///
				, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
				ytitle("${ytit}") ylabel(,angle(0) format(${`outcome'_fmt}))	///
				xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
				${ylab} ///
				legend(size(small) order (2 4) label(2 "Cutoff = 31") label(4 "Cutoff = 50"))
			
			
		graph export "output/coef_plot_baseline_`outcome'.pdf", as(pdf) replace

end


*---------------------------------------*
*PROGRAM TO COMPARE ACROSS RACE, SEX, and COHORT
*---------------------------------------*
cap program drop comparegroups
program define comparegroups
syntax [, load(str) source(str) outcome(str) ylab(str) het(str) ]
			
	global racecat		"white black"
	global white_lbl	"White"
	global black_lbl	"Black"

	global cohortcat	"B A"
	global A_lbl		"1990-2000 Cohorts"
	global B_lbl	"2001-2011 Cohorts"
	
	global cut1	"31"
	global cut2 "50"

	
if "`ylab'" == "none" local ylab 
else local ylab `ylab' 

if "`outcome'" == "any_dis" global ytit Any Disability
if "`outcome'" == "any_ssissdi" global ytit Any SSI or SSDI
if "`outcome'" == "any_worklim" global ytit Any Signif. Disability
if "`outcome'" == "dis_tot" global ytit Disability (Dollars)
if "`outcome'" == "ssissdi_tot" global ytit SSI or SSDI (Dollars)

global dis_tot_fmt %9.0fc
global ssissdi_tot_fmt %9.0fc

foreach x in  any_dis any_ssissdi any_worklim  {
	global `x'_fmt %5.2f
}

			foreach s in ${`het'cat}{
				di "het=`het', outcome=`outcome', s=`s'"

global loader ./output/main-estimates/`outcome'/`s'/graphdata_`outcome'_`s'
				use "${loader}" if y<=19, clear
				drop if y == -2
				local z 2sls_quad 
					preserve
					keep y *_`z' 
					rename (beta_cut31_`z' beta_cut50_`z' se_cut31_`z' se_cut50_`z')	///
						(b31_`s' b50_`s' se31_`s' se50_`s')
						gen ub31_`s' = b31_`s' + 1.96*se31_`s'
						gen lb31_`s' = b31_`s' - 1.96*se31_`s'
						gen ub50_`s' = b50_`s' + 1.96*se50_`s'
						gen lb50_`s' = b50_`s' - 1.96*se50_`s'
					tempfile het`s'_`z'
					save `het`s'_`z'', replace
					restore
				}
			
			local s1 `: word 1 of ${`het'cat}'
			local s2 `: word 2 of ${`het'cat}'
			use `het`s1'_`z'', clear
			merge 1:1 y using `het`s2'_`z'', nogen
			
			gen y1 = y - 0.1
			gen y2 = y + 0.2
			
			foreach cut in 31 50 {
				tw 	(rcap ub`cut'_`s1' lb`cut'_`s1' y1, lcolor(gs7) lp(longdash) lw(thin))	///
					(line b`cut'_`s1' y1, lcolor(gs7) lp(longdash) lw(medthick))	///
					(rcap ub`cut'_`s2' lb`cut'_`s2' y2, lcolor(black) lp(solid) lw(thin))	///
					(line b`cut'_`s2' y2, lcolor(black) lp(shortdash) lw(medthick))	///
					, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
					ytitle("${ytit}") ylabel(,angle(0) format(${`outcome'_fmt}))	///
					xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
					legend(size(small) order (4 2) label(2 "${`s1'_lbl} (Cutoff=`cut')") label(4 "${`s2'_lbl} (Cutoff=`cut')"))
					graph export "output/compare-`het'-`outcome'-cut`cut'.pdf", as(pdf) replace
				}	

end


*---------------------------------------*
*---------------------------------------*
*FIGURE 5 PANEL B
*---------------------------------------*
*---------------------------------------*

	foreach outcome in  dis_tot  {
comparecut12, load(main-estimates) source(oema) outcome(`outcome') ylab(none) 
	}


*---------------------------------------*
*---------------------------------------*
*FIGURE 8 PANEL B 
*---------------------------------------*
*---------------------------------------*

	foreach outcome in  dis_tot  { 
comparegroups, load(main-estimates) source(oema) outcome(`outcome') ylab(none) het(race) 
	}


*---------------------------------------*
*---------------------------------------*
*FIGURE A9
*---------------------------------------*
*---------------------------------------*

foreach outcome in dis_tot any_dis  { 

if "`outcome'" == "any_dis" global ytit Any Disability
if "`outcome'" == "dis_tot" global ytit Disability (Dollars)

global wages_plus_os_fmt %9.0fc
global dis_tot_fmt %9.0fc

foreach x in anywages anywagesor1099 anyf1098T any_dis  {
	global `x'_fmt %5.2f
}
		
		if "`outcome'" == "dis_tot" | "`outcome'" == "any_dis" {
		local source oema 
		local load main-estimates-randr
		use "./output/main-estimates/`outcome'/graphdata_`outcome'" if y<=19 & y!=-2, clear
		}
		
		
		g yorig = y
		replace y = y
		gen y2 = y
		
		foreach model in 2sls_quad{
				
if "`outcome'" != "wages_plus_os" {
			
			tw 	///
				(line mean_cut31_`model' y, lcolor(black) lp(dash) lw(medthick))	///
				(line mean_cut50_`model' y2, lcolor(gs7) lw(medthick))	///
				, xline(0, lpattern(dot)) ///
				ytitle("${ytit}") ylabel(,angle(0) format(${`outcome'_fmt}))	///
				xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
				legend(size(small) order (1 2) label(1 "Cutoff = 31") label(2 "Cutoff = 50"))
				
		graph export "output/mean_plot_`outcome'_`model'.pdf", as(pdf) replace
}
			
			}
		}

	
*---------------------------------------*
*---------------------------------------*
*FIGURE A13
*---------------------------------------*
*---------------------------------------*

	foreach outcome in any_dis any_worklim any_ssissdi ssissdi_tot {
comparecut12, load(main-estimates) source(oema) outcome(`outcome') ylab(none) 
	}

	
*---------------------------------------*
*---------------------------------------*
*FIGURE A18 
*---------------------------------------*
*---------------------------------------*

foreach outcome in  any_dis  {
	
if "`outcome'" == "any_dis" {
local ytit Any Disability
local fmt %5.2f
}


if "`outcome'" == "any_ssissdi" | "`outcome'" == "any_dis" |  "`outcome'" == "dis_tot"     use "./output/main-estimates/cf_`outcome'/white/graphdata_cf_`outcome'_white" if y<=19 & y != -2, clear

foreach var of varlist * {
	rename `var' `var'_w
}
rename y_w y

preserve 


if "`outcome'" == "any_ssissdi" | "`outcome'" == "any_dis" |  "`outcome'" == "dis_tot"  use "./output/main-estimates/cf_`outcome'/black/graphdata_cf_`outcome'_black" if y<=19 & y != -2, clear

foreach var of varlist * {
	rename `var' `var'_b
}
rename y_b y
tempfile a 
save `a', replace
restore

merge 1:1 y using `a', nogen

cap gen y1 = y - 0.1
cap gen y2 = y + 0.2

foreach cut in 31 50 {
g ub_`cut'_w = beta_cut`cut'_2sls_quad_w + 1.96*se_cut`cut'_2sls_quad_w
g lb_`cut'_w = beta_cut`cut'_2sls_quad_w - 1.96*se_cut`cut'_2sls_quad_w

g ub_`cut'_b = beta_cut`cut'_2sls_quad_b + 1.96*se_cut`cut'_2sls_quad_b
g lb_`cut'_b = beta_cut`cut'_2sls_quad_b - 1.96*se_cut`cut'_2sls_quad_b

				tw 	(rcap ub_`cut'_w lb_`cut'_w y1, lcolor(gs7) lp(longdash) lw(thin))	///
					(line beta_cut`cut'_2sls_quad_w y1, lcolor(gs7) lp(longdash) lw(medthick))	///
					(rcap ub_`cut'_b lb_`cut'_b y2, lcolor(black) lp(solid) lw(thin))	///
					(line beta_cut`cut'_2sls_quad_b y2, lcolor(black) lp(shortdash) lw(medthick))	///
					, yline(0, lpattern(dot)) xline(0, lpattern(dot))	///
					ytitle("`ytit'") ylabel(,angle(0) format(`fmt'))	///
					xtitle("Years Relative to Application") xlabel(-1(2)19) xtick(-1(1)19)	///
					legend(size(small) order (4 2) label(2 "White (Cutoff=`cut')") label(4 "Black (Cutoff=`cut')"))
					graph export "output/cf-earnings-`outcome'-cut`cut'.pdf", as(pdf) replace
}
}




*---------------------------------------*
*---------------------------------------*
*FIGURE A19 and A20
*---------------------------------------*
*---------------------------------------*

	foreach outcome in  any_worklim dis_tot  {
comparegroups, load(main-estimates) source(oema) outcome(`outcome') ylab(none) het(race) 
	}


*---------------------------------------*
*---------------------------------------*
*FIGURE A29 and A30 
*---------------------------------------*
*---------------------------------------*

foreach group in   cohort  {  
	foreach outcome in dis_tot any_dis any_worklim  {
comparegroups, load(main-estimates) source(oema) outcome(`outcome') ylab(none) het(`group') 
}
}
